﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_Game_Framework
{
	public class Border : Control
	{
		public enum Sides { All, Vertical, Horizontal, Top, Bottom, Left, Right, TopLess, BottomLess, LeftLess, RightLess }

		Texture2D bordertopleft;
		Texture2D bordertopright;
		Texture2D borderbottomleft;
		Texture2D borderbottomright;
		Texture2D borderleft;
		Texture2D borderright;
		Texture2D bordertop;
		Texture2D borderbottom;

		bool drawTop;
		bool drawBottom;
		bool drawLeft;
		bool drawRight;

		Vector2 size;

		public override Vector2 Size
		{
			get { return size; }
		}

		public Border(object parent, String name, Vector2 position, Vector2 size, Sides sides, Theme.Border theme)
			: base(name, parent, position)
		{
			this.size = size;
			this.bordertopleft = theme.bordertopleft;
			this.bordertopright = theme.bordertopright;
			this.borderbottomleft = theme.borderbottomleft;
			this.borderbottomright = theme.borderbottomright;
			this.borderleft = theme.borderleft;
			this.borderright = theme.borderright;
			this.bordertop = theme.bordertop;
			this.borderbottom = theme.borderbottom;

			SetSides(sides);
		}

		private void SetSides(Sides sides)
		{
			switch (sides)
			{
				case Sides.All:
					drawTop = true;
					drawBottom = true;
					drawLeft = true;
					drawRight = true;
					break;
				case Sides.Vertical:
					drawLeft = true;
					drawRight = true;
					break;
				case Sides.Horizontal:
					drawTop = true;
					drawBottom = true;
					break;
				case Sides.Top:
					drawTop = true;
					break;
				case Sides.Bottom:
					drawBottom = true;
					break;
				case Sides.Left:
					drawLeft = true;
					break;
				case Sides.Right:
					drawRight = true;
					break;
				case Sides.TopLess:
					drawBottom = true;
					drawLeft = true;
					drawRight = true;
					break;
				case Sides.BottomLess:
					drawTop = true;
					drawLeft = true;
					drawRight = true;
					break;
				case Sides.LeftLess:
					drawTop = true;
					drawBottom = true;
					drawRight = true;
					break;
				case Sides.RightLess:
					drawTop = true;
					drawBottom = true;
					drawLeft = true;
					break;
				default:
					break;
			}
		}

		internal override void Draw(SpriteBatch spriteBatch)
		{
			if (Visible)
			{
				DrawTop(spriteBatch);
				DrawSides(spriteBatch);
				DrawBottom(spriteBatch);
			}
		}

		private void DrawTop(SpriteBatch spriteBatch)
		{
			if (drawTop || drawLeft)
			{
				spriteBatch.Draw(bordertopleft, WindowPosition, Color.White);
			}

			if (drawTop)
			{
				int numberSlices = (int)((Size.X - bordertopleft.Width - bordertopright.Width) / bordertop.Width);
				Vector2 startPosition = new Vector2(WindowPosition.X + bordertopleft.Width, WindowPosition.Y);
				DrawHorizontalSlices(spriteBatch, bordertop, numberSlices, startPosition);
			}

			if (drawTop || drawRight)
			{
				Vector2 toprightPosition = new Vector2(WindowPosition.X + Size.X - bordertopright.Width, WindowPosition.Y);
				spriteBatch.Draw(bordertopright, toprightPosition, Color.White);
			}
		}

		private void DrawSides(SpriteBatch spriteBatch)
		{
			if (drawLeft)
			{
				Vector2 leftBorderPosition = new Vector2(WindowPosition.X, WindowPosition.Y + bordertopleft.Height);
				int leftNumberOfSlices = (int)((Size.Y - bordertopleft.Height - borderbottomleft.Height) / borderleft.Height);
				DrawVerticalSlices(spriteBatch, borderleft, leftNumberOfSlices, leftBorderPosition);
			}
			if (drawRight)
			{
				Vector2 rightBorderPosition = new Vector2(WindowPosition.X + Size.X - borderright.Width, WindowPosition.Y + bordertopright.Height);
				int rightNumberOfSlices = (int)((Size.Y - bordertopright.Height - borderbottomright.Height) / borderright.Height);
				DrawVerticalSlices(spriteBatch, borderright, rightNumberOfSlices, rightBorderPosition);
			}
		}

		private void DrawBottom(SpriteBatch spriteBatch)
		{
			if (drawBottom || drawLeft)
			{
				Vector2 bottomleftPosition = new Vector2(WindowPosition.X, WindowPosition.Y + Size.Y - borderbottomleft.Height);
				spriteBatch.Draw(borderbottomleft, bottomleftPosition, Color.White);
			}

			if (drawBottom)
			{
				int numberSlices = (int)((Size.X - borderbottomleft.Width - borderbottomright.Width) / borderbottom.Width);
				Vector2 startPosition = new Vector2(WindowPosition.X + borderbottomleft.Width, WindowPosition.Y + Size.Y - borderbottom.Height);
				DrawHorizontalSlices(spriteBatch, borderbottom, numberSlices, startPosition);
			}

			if (drawBottom || drawRight)
			{
				Vector2 bottomrightPosition = new Vector2(WindowPosition.X + Size.X - borderbottomright.Width, WindowPosition.Y + Size.Y - borderbottomright.Height);
				spriteBatch.Draw(borderbottomright, bottomrightPosition, Color.White);
			}
		}

		private void DrawHorizontalSlices(SpriteBatch spriteBatch, Texture2D texture, int numberOfSlices, Vector2 startPosition)
		{
			for (int i = 0; i < numberOfSlices; i++)
			{
				spriteBatch.Draw(texture, startPosition, Color.White);
				startPosition.X += texture.Width;
			}
		}

		private void DrawVerticalSlices(SpriteBatch spriteBatch, Texture2D texture, int numberOfSlices, Vector2 startPosition)
		{
			for (int i = 0; i < numberOfSlices; i++)
			{
				spriteBatch.Draw(texture, startPosition, Color.White);
				startPosition.Y += texture.Height;
			}
		}
	}
}
